package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.Classify_ad_Activity;
import com.qianfeng.wuyang.himalaya.activity.MainActivity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.DiscoveryInfoEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *recommend 里第二个 item，听资讯 今日最火 等
 * Created by WangQing on 2016/12/7.
 */

public class RecommendDiscoveryAdapter extends RecyclerView.Adapter<RecommendDiscoveryAdapter.ViewHolder> implements View.OnClickListener {
    private List<DiscoveryInfoEntity> list;
    private Context context;
    private MainActivity activity;

    public RecommendDiscoveryAdapter(List<DiscoveryInfoEntity> list, Context context,MainActivity activity) {
        this.list = new ArrayList<>();
        this.context = context;
        this.activity = activity;
    }

    public void addDatas(Collection<? extends DiscoveryInfoEntity> collection){
        list.addAll(collection);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_recomenddiscovery_include,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (list.get(position).getTitle()!=null ) {
            holder.textView.setText(list.get(position).getTitle());
        }

        if (list.get(position).getCoverPath()!=null ) {
            Picasso.with(context).load(list.get(position).getCoverPath()).into(holder.imageView);

            holder.layout.setOnClickListener(this);
        }



    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View view) {
        int position=((RecyclerView)view.getParent()).getChildAdapterPosition(view);

//        Toast.makeText(context,"发现 第"+position,Toast.LENGTH_SHORT).show();

        //今日最火
        if (list.get(position).getContentType().equals("ranking_list")) {
            String key=list.get(position).getProperties().getKey();
            activity.drawerLayout.openDrawer(Gravity.RIGHT);
            FrameLayout frameLayout = (FrameLayout) activity.findViewById(R.id.drawer_container);
            View viewgroup = LayoutInflater.from(activity).inflate(R.layout.fragment_today_hottest,frameLayout,false);
            frameLayout.addView(viewgroup);

            TabLayout tabLayout = (TabLayout) viewgroup.findViewById(R.id.discovery_today_tablayout);
            final ViewPager viewPager = (ViewPager) viewgroup.findViewById(R.id.discovery_today_viewpager);

            tabLayout.setupWithViewPager(viewPager);

            TodayHottestAdapter adapter = new TodayHottestAdapter(activity.getSupportFragmentManager(),context,key);
            viewPager.setAdapter(adapter);

            //滑动冲突
            viewPager.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            if (motionEvent.getX() > view.getWidth()/5) {
                                viewPager.requestDisallowInterceptTouchEvent(true);
                            }
                            break;
                    }
                    return false;
                }
            });
//
        }

        //网页
        if (!list.get(position).getUrl().equals("")) {
            Intent intent = new Intent(context, Classify_ad_Activity.class);
            intent.putExtra("url",list.get(position).getUrl());
            context.startActivity(intent);
        }
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{
        private  LinearLayout layout;
        private ImageView imageView;
        private  TextView textView;


        public ViewHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.recommend_discovery_img);
            textView = (TextView) itemView.findViewById(R.id.recommend_discovery_tv);
            layout = (LinearLayout) itemView.findViewById(R.id.discovery_include_layout);
        }
    }
}
